package com.work.saturn.account;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.coorchice.library.SuperTextView;
import com.immo.libcommon.base.BaseActivity;
import com.immo.libcommon.constant.BaseUrl;
import com.immo.libcommon.http.NetworkRequest;
import com.immo.libcommon.http.RequestSuccess;
import com.immo.libcommon.utils.InputValidationUtils;
import com.work.saturn.R;
import com.work.saturn.bean.CheckSMSBean;
import com.work.saturn.bean.GetCodeBean;
import com.work.saturn.bean.RegisterBean;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @content 注册页面
 * @date 2018/7/10
 */
public class RegisterActivity extends BaseActivity {
    /**
     * 用户名
     */
    @BindView(R.id.register_input_username)
    EditText username;
    /**
     * 验证码
     */
    @BindView(R.id.register_input_code)
    EditText inputCode;
    /**
     * 获取验证码
     */
    @BindView(R.id.register_input_get_code)
    SuperTextView getCode;
    /**
     * 密码
     */
    @BindView(R.id.register_input_password)
    EditText password;
    /**
     * 手机号
     */
    @BindView(R.id.register_input_phone)
    EditText phoneEt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //禁止进入时弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        ButterKnife.bind(this);
    }

    /**
     * 输入的本地判断
     *
     * @return true 通过 false 不通过
     */
    private boolean isCheck() {
        String user = username.getText().toString();
        String phone=phoneEt.getText().toString();
        String ps = password.getText().toString();
        String code = inputCode.getText().toString();
        if (user.length() == 0) {
            toast("用户名不能为空");
            return false;
        }
        if (phone.length() == 0) {
            toast("手机号码不能为空");
            return false;
        }
        if (!phone.matches(InputValidationUtils.Phone)) {
            toast("手机号码格式错误");
            return false;
        }
        if (ps.length() == 0) {
            toast("密码不能为空");
            return false;
        }
        if (code.length() == 0) {
            toast("验证码不能为空");
        }

        return true;
    }

    /**
     * 获取验证码
     */
    private void getSMSCode() {
        new NetworkRequest(new RequestSuccess() {
            @Override
            public void loadHttp(Object object, String data) {
                if (object instanceof GetCodeBean) {
                    GetCodeBean getCodeBean = (GetCodeBean) object;
                    toast(getCodeBean.getData());
                }
            }
        }).requestGet(BaseUrl.GETSMSCODE + "?phone=" + phoneEt.getText().toString(),
                this, GetCodeBean.class, false, false);
    }

    /**
     * 校验验证码
     */
    private void checkSMSCode() {
        HashMap<String, String> params = new HashMap<>();
        params.put("phone", phoneEt.getText().toString());
        params.put("code", inputCode.getText().toString());
        new NetworkRequest(new RequestSuccess() {
            @Override
            public void loadHttp(Object object, String data) {
                if (object instanceof CheckSMSBean) {
                    CheckSMSBean checkSMSBean = (CheckSMSBean) object;
                    if (checkSMSBean.getMsg().equals("success")) {
                        registerNet();
                    } else {
                        toast("验证码错误");
                    }
                }
            }
        }).requestPostKey(BaseUrl.CHECKSMSCODE,
                this, params, CheckSMSBean.class, false, false);
    }

    /**
     * 注册
     */
    private void registerNet() {
        HashMap<String, String> params = new HashMap<>();
        params.put("username", username.getText().toString());
        params.put("role", "user");
        params.put("email", "");
        params.put("phone", phoneEt.getText().toString());
        params.put("password", password.getText().toString());
        new NetworkRequest(new RequestSuccess() {
            @Override
            public void loadHttp(Object object, String data) {
                if (object instanceof RegisterBean) {
                    RegisterBean registerBean = (RegisterBean) object;
                    if (registerBean.isSuccess()) {
                        toast("注册成功");
                        finish();
                    }
                }
            }
        }).requestPost(BaseUrl.REGISTER,
                this, JSON.toJSONString(params), RegisterBean.class, true, false);
    }

    @OnClick({R.id.register_back, R.id.register_input_get_code, R.id.register_submit, R.id.register_to_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //返回
            case R.id.register_back:
                finish();
                break;
            case R.id.register_input_get_code:
                //获取验证码
                String phone = phoneEt.getText().toString();
                if (!phone.isEmpty() && phone.matches(InputValidationUtils.Phone)) {
                    getSMSCode();
                } else {
                    toast("手机号码格式错误");
                }
                break;
            case R.id.register_submit:
                //提交
                if (isCheck()) {
                    checkSMSCode();
                }
                break;
            case R.id.register_to_register:
                //跳到登录
                finish();
                break;
        }
    }
}
